﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace xxxy.DAL
{
	/// <summary>
	/// 数据访问类:personalInfo
	/// </summary>
	public partial class personalInfo
	{
		public personalInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("pID", "personalInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int pID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from personalInfo");
			strSql.Append(" where pID=@pID ");
			SqlParameter[] parameters = {
					new SqlParameter("@pID", SqlDbType.Int,4)			};
			parameters[0].Value = pID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(xxxy.Model.personalInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into personalInfo(");
			strSql.Append("pID,pDataName,pIssuingTime,pUploadTime,pDepartment,pAddress,pAttachments1,pAttachments2,pRemarks,pStation,pUploadName)");
			strSql.Append(" values (");
			strSql.Append("@pID,@pDataName,@pIssuingTime,@pUploadTime,@pDepartment,@pAddress,@pAttachments1,@pAttachments2,@pRemarks,@pStation,@pUploadName)");
			SqlParameter[] parameters = {
					new SqlParameter("@pID", SqlDbType.Int,4),
					new SqlParameter("@pDataName", SqlDbType.NVarChar,50),
					new SqlParameter("@pIssuingTime", SqlDbType.Date,3),
					new SqlParameter("@pUploadTime", SqlDbType.Date,3),
					new SqlParameter("@pDepartment", SqlDbType.VarChar,60),
					new SqlParameter("@pAddress", SqlDbType.VarChar,100),
					new SqlParameter("@pAttachments1", SqlDbType.VarChar,100),
					new SqlParameter("@pAttachments2", SqlDbType.VarChar,100),
					new SqlParameter("@pRemarks", SqlDbType.VarChar,100),
					new SqlParameter("@pStation", SqlDbType.Int,4),
					new SqlParameter("@pUploadName", SqlDbType.Int,4)};
			parameters[0].Value = model.pID;
			parameters[1].Value = model.pDataName;
			parameters[2].Value = model.pIssuingTime;
			parameters[3].Value = model.pUploadTime;
			parameters[4].Value = model.pDepartment;
			parameters[5].Value = model.pAddress;
			parameters[6].Value = model.pAttachments1;
			parameters[7].Value = model.pAttachments2;
			parameters[8].Value = model.pRemarks;
			parameters[9].Value = model.pStation;
			parameters[10].Value = model.pUploadName;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(xxxy.Model.personalInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update personalInfo set ");
			strSql.Append("pDataName=@pDataName,");
			strSql.Append("pIssuingTime=@pIssuingTime,");
			strSql.Append("pUploadTime=@pUploadTime,");
			strSql.Append("pDepartment=@pDepartment,");
			strSql.Append("pAddress=@pAddress,");
			strSql.Append("pAttachments1=@pAttachments1,");
			strSql.Append("pAttachments2=@pAttachments2,");
			strSql.Append("pRemarks=@pRemarks,");
			strSql.Append("pStation=@pStation,");
			strSql.Append("pUploadName=@pUploadName");
			strSql.Append(" where pID=@pID ");
			SqlParameter[] parameters = {
					new SqlParameter("@pDataName", SqlDbType.NVarChar,50),
					new SqlParameter("@pIssuingTime", SqlDbType.Date,3),
					new SqlParameter("@pUploadTime", SqlDbType.Date,3),
					new SqlParameter("@pDepartment", SqlDbType.VarChar,60),
					new SqlParameter("@pAddress", SqlDbType.VarChar,100),
					new SqlParameter("@pAttachments1", SqlDbType.VarChar,100),
					new SqlParameter("@pAttachments2", SqlDbType.VarChar,100),
					new SqlParameter("@pRemarks", SqlDbType.VarChar,100),
					new SqlParameter("@pStation", SqlDbType.Int,4),
					new SqlParameter("@pUploadName", SqlDbType.Int,4),
					new SqlParameter("@pID", SqlDbType.Int,4)};
			parameters[0].Value = model.pDataName;
			parameters[1].Value = model.pIssuingTime;
			parameters[2].Value = model.pUploadTime;
			parameters[3].Value = model.pDepartment;
			parameters[4].Value = model.pAddress;
			parameters[5].Value = model.pAttachments1;
			parameters[6].Value = model.pAttachments2;
			parameters[7].Value = model.pRemarks;
			parameters[8].Value = model.pStation;
			parameters[9].Value = model.pUploadName;
			parameters[10].Value = model.pID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int pID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from personalInfo ");
			strSql.Append(" where pID=@pID ");
			SqlParameter[] parameters = {
					new SqlParameter("@pID", SqlDbType.Int,4)			};
			parameters[0].Value = pID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string pIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from personalInfo ");
			strSql.Append(" where pID in ("+pIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public xxxy.Model.personalInfo GetModel(int pID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 pID,pDataName,pIssuingTime,pUploadTime,pDepartment,pAddress,pAttachments1,pAttachments2,pRemarks,pStation,pUploadName from personalInfo ");
			strSql.Append(" where pID=@pID ");
			SqlParameter[] parameters = {
					new SqlParameter("@pID", SqlDbType.Int,4)			};
			parameters[0].Value = pID;

			xxxy.Model.personalInfo model=new xxxy.Model.personalInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public xxxy.Model.personalInfo DataRowToModel(DataRow row)
		{
			xxxy.Model.personalInfo model=new xxxy.Model.personalInfo();
			if (row != null)
			{
				if(row["pID"]!=null && row["pID"].ToString()!="")
				{
					model.pID=int.Parse(row["pID"].ToString());
				}
				if(row["pDataName"]!=null)
				{
					model.pDataName=row["pDataName"].ToString();
				}
				if(row["pIssuingTime"]!=null && row["pIssuingTime"].ToString()!="")
				{
					model.pIssuingTime=DateTime.Parse(row["pIssuingTime"].ToString());
				}
				if(row["pUploadTime"]!=null && row["pUploadTime"].ToString()!="")
				{
					model.pUploadTime=DateTime.Parse(row["pUploadTime"].ToString());
				}
				if(row["pDepartment"]!=null)
				{
					model.pDepartment=row["pDepartment"].ToString();
				}
				if(row["pAddress"]!=null)
				{
					model.pAddress=row["pAddress"].ToString();
				}
				if(row["pAttachments1"]!=null)
				{
					model.pAttachments1=row["pAttachments1"].ToString();
				}
				if(row["pAttachments2"]!=null)
				{
					model.pAttachments2=row["pAttachments2"].ToString();
				}
				if(row["pRemarks"]!=null)
				{
					model.pRemarks=row["pRemarks"].ToString();
				}
				if(row["pStation"]!=null && row["pStation"].ToString()!="")
				{
					model.pStation=int.Parse(row["pStation"].ToString());
				}
				if(row["pUploadName"]!=null && row["pUploadName"].ToString()!="")
				{
					model.pUploadName=int.Parse(row["pUploadName"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select pID,pDataName,pIssuingTime,pUploadTime,pDepartment,pAddress,pAttachments1,pAttachments2,pRemarks,pStation,pUploadName ");
			strSql.Append(" FROM personalInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" pID,pDataName,pIssuingTime,pUploadTime,pDepartment,pAddress,pAttachments1,pAttachments2,pRemarks,pStation,pUploadName ");
			strSql.Append(" FROM personalInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM personalInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.pID desc");
			}
			strSql.Append(")AS Row, T.*  from personalInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "personalInfo";
			parameters[1].Value = "pID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

